<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;

// 微信用户
class UserController extends Controller
{
    // 列表
    public function index(Request $request)
    {
        $keywords = $request->input('keywords', ''); // 查询关键字
        $user_auth_id = $request->input('user_auth_id', ''); // 审批角色
        $is_all = $request->input('is_all', 0); //不分页获取全部数据

        if($is_all){
            $data = User::query()
                ->when($keywords, function ($query) use ($keywords) {
                    $query->where('username', 'like', "%{$keywords}%");
                })
                ->when($user_auth_id, function ($query) use ($user_auth_id) {
                    $query->where('user_auth_id', 'like', "%{','.$user_auth_id,','}%");
                })
                ->with('user_role:id,name')
                // ->where($this->getAppWhere())
                 ->orderBy('id')
                ->get()
                ->toArray();
        }else{
            $data = User::query()
            ->when($keywords, function ($query) use ($keywords) {
                $query->where('username', 'like', "%{$keywords}%");
            })
            ->when($user_auth_id, function ($query) use ($user_auth_id) {
                $query->where('user_auth_id', 'like',"%{','.$user_auth_id','}%");
            })
            ->with('user_role:id,name')
            // ->where($this->getAppWhere())
            ->orderByDesc('id')
            ->paginate(env('PAGE_SIZE', 10));
        }
        return showData($data);    
    }

    // 新增
    public function store(Request $request)
    {
        // 验证
        $validate = Validator::make($request->all(), [
            'phone' => 'required',
            'user_role_ids' => 'required',
            'now_role_id' => 'required',
            'username' => 'required',
            'status' => 'required',
            'street_id' => 'required',
            'village_id' => 'required',
            'project_id' => 'required',
            'segment_id' => 'required',
            'grid_group_id' => 'required',
            'apply_status' => 'required',
            'menus_auth_id' => 'required',
        ], [
            'phone.required' => '请输入手机号',
            'user_role_ids.required' => '请输入可选身份',
            'now_role_id.required' => '请输入当前项目',
            'username.required' => '请输入真实姓名',
            'status.required' => '请输入账号状态',
            'street_id.required' => '请输入街道',
            'village_id.required' => '请输入村庄',
            'project_id.required' => '请输入项目',
            'segment_id.required' => '请输入号段',
            'grid_group_id.required' => '请输入网格组',
            'apply_status.required' => '请输入申请状态',
            'menus_auth_id.required' => '请输入菜单角色',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $data = $request->all();
        $data['user_auth_id']=','.$data['user_auth_id'].',';

        try {
            $update = User::query()->create($data);
            return showSuccess('新增成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 编辑
    public function update($userId, Request $request)
    {
        if (empty($userId)) {
            return showError('id不能为空!');
        }
        // 验证
        $validate = Validator::make($request->all(), [
            'phone' => 'required',
            'user_role_ids' => 'required',
            'now_role_id' => 'required',
            'username' => 'required',
            'status' => 'required',
            'street_id' => 'required',
            'village_id' => 'required',
            'project_id' => 'required',
            'segment_id' => 'required',
            'grid_group_id' => 'required',
            'id_obverse_img' => 'required',
            'id_reverse_img' => 'required',
            'signatures_img' => 'required',
            'apply_img' => 'required',
            'apply_status' => 'required',
            'menus_auth_id' => 'required',
        ], [
            'phone.required' => '请输入手机号',
            'user_role_ids.required' => '请输入可选身份',
            'now_role_id.required' => '请输入当前项目',
            'username.required' => '请输入真实姓名',
            'status.required' => '请输入账号状态',
            'street_id.required' => '请输入街道',
            'village_id.required' => '请输入村庄',
            'project_id.required' => '请输入项目',
            'segment_id.required' => '请输入号段',
            'grid_group_id.required' => '请输入网格组',
            'id_obverse_img.required' => '请输入身份证正面照片',
            'id_reverse_img.required' => '请输入身份证反面照片',
            'signatures_img.required' => '请输入电子签',
            'apply_img.required' => '请输入申请书',
            'apply_status.required' => '请输入申请状态',
            'menus_auth_id.required' => '请输入菜单角色',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $data = $validate->getData();

        $apply_status = User::query()->where('id', $userId)->value('apply_status');
        if($apply_status!=$data['apply_status']){//有设置审批状态
            if($data['apply_status']==2){
                $data['status']=1;
            }else{
                $data['status']=0;
            }
        }

        $data['user_auth_id']=','.$data['user_auth_id'].',';

        try {
            $update = User::query()->where('id', $userId)->update($data);
            return showSuccess('编辑成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 详情
    public function show($id)
    {
        if (empty($id)) {
            return showError('参数错误');
        }
   
        $data = User::query()
            ->where('id', $id)
            ->first();

        if (empty($data)) {
            return showError('获取信息失败');
        }

        $data['id_obverse_img_url']=$data['id_obverse_img']?$this->getImageUrl($data['id_obverse_img']):'';
        $data['id_reverse_img_url']=$data['id_reverse_img']?$this->getImageUrl($data['id_reverse_img']):'';
        $data['signatures_img_url']=$data['signatures_img']?$this->getImageUrl($data['signatures_img']):'';
        $data['apply_img_url']=$data['apply_img']?$this->getImageUrl($data['apply_img']):'';
        $data['user_auth_id'] = trim($data['user_auth_id'],",");
        return showData($data);
    }
}

